package com.bignerdranch.android.ecar.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.bignerdranch.android.ecar.Bean.UsedCarBean;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.RequestUtil;
import com.bignerdranch.android.ecar.Utils.URLBank;
import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class UsedCarDetailActivity extends BaseActivity implements View.OnClickListener {

    private ImageButton used_car_detail_back;
    private SliderLayout slider;
    private PagerIndicator custom_indicator;
    private List<UsedCarBean> mList = new ArrayList<UsedCarBean>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_used_car_wan_detial);
        initView();
        initData();
    }

    private void initData() {
        String id = getIntent().getStringExtra("id");
        String url = URLBank.teacher_url + URLBank.queryById_url + "?id=" + id;
        RequestUtil.executeJSONObject(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject o) {
                try {
                    String img_string = o.getJSONObject("data").getString("image");
                    String type = o.getJSONObject("data").getString("type");
                    String[] img_array = img_string.split(",");
//                    Log.e("--in--", img_array.length + "");


                    for (int i = 0; i < img_array.length; i++) {
                        UsedCarBean bean = new UsedCarBean(URLBank.teacher_url + URLBank.car_url + img_array[i], type);
                        mList.add(bean);
//                        Log.e("--car--", URLBank.teacher_url + URLBank.car_url + img_array[i]);
                    }

                    initSlider();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });


    }
    private void initSlider() {

        for (final UsedCarBean bean:mList){
            TextSliderView textSliderView = new TextSliderView(this);
            textSliderView
                    .description(bean.getType())
                    .image(bean.getImage_name());

            textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    Toast.makeText(UsedCarDetailActivity.this,bean.getType(),Toast.LENGTH_SHORT).show();
                }
            });

            slider.addSlider(textSliderView);
        }
        slider.setCustomIndicator(custom_indicator);
        slider.setCustomAnimation(new DescriptionAnimation());
        slider.setPresetTransformer(SliderLayout.Transformer.CubeIn);
        slider.setDuration(3000);
    }

    @Override
    protected void onStop() {
        super.onStop();
        slider.stopAutoCycle();
    }

    private void initView() {
        used_car_detail_back = (ImageButton) findViewById(R.id.used_car_detail_back);
        slider = (SliderLayout) findViewById(R.id.image_title);
        custom_indicator = (PagerIndicator) findViewById(R.id.custom_indicator);

        used_car_detail_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.used_car_detail_back:
                finish();
                break;
        }
    }
}
